package cn.westv.scps.web.controller.appversion;

import cn.westv.scps.domain.AppVersionEntity;
import cn.westv.scps.service.appversion.IAppVersionService;
import cn.westv.scps.utils.Page;
import cn.westv.scps.utils.StringUtils;
import cn.westv.scps.web.controller.appversion.vo.AppVersionVO;
import cn.westv.scps.web.controller.appversion.vo.SearchForm;
import cn.westv.scps.web.controller.base.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/17.
 */
@Controller
@RequestMapping("/appversion")
public class AppVersionController extends AbstractController<AppVersionEntity> {
    @Autowired
    private IAppVersionService appVersionService;

    @RequestMapping("/index.cgi")
    public String toIndex(){
        return "forward:/WEB-INF/page/sys/appversion/index.jsp";
    }

    @RequestMapping("/checkupdate/{appType}")
    @ResponseBody
    public Map<String,Object> getLatestVersion(@PathVariable String appType,String versionNo){
        boolean needForce = false;
        if(StringUtils.isNotBlank(versionNo)){
            AppVersionEntity latestForceVersion = appVersionService.findLatestForceVersion(appType);
            if(latestForceVersion != null ){
                String forcedNo = latestForceVersion.getVersionNo();
                String[] noArray = forcedNo.split("\\.");
                String[] versionNoArray = versionNo.split("\\.");
                //需要强制更新

                if(versionNoArray != null && versionNoArray.length > 0){
                    for(int i=0 ; i < versionNoArray.length ; i++){
                        String versionLevel = versionNoArray[i];
                        //如果当前版本小于需要强制更新的版本 则
                        if(i < noArray.length && Integer.valueOf(versionLevel) < Integer.valueOf(noArray[i])){
                            needForce = true;
                            break;
                        }
                    }
                }

            }
        }

        AppVersionEntity entity = appVersionService.findLatest(appType);
        if(needForce){
            entity.setIsForce(AppVersionEntity.IS_FORCE_TRUE);
        }
        AppVersionVO vo = AppVersionVO.poToVo(entity);
        return super.ajaxReturnMap(OP_STATUS_SUCCESS,"",vo);
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> queryPage(int rows, int page, AppVersionEntity entity){
        Page<AppVersionEntity> pageEn = appVersionService.findByPage(page,rows,entity);
        Map<String,Object> pageMap = new HashMap<String,Object>();
        List<AppVersionVO> voList = new ArrayList<AppVersionVO>();
        if(pageEn.getResults() != null && pageEn.getResults().size() > 0){
            for(AppVersionEntity appVersionEntity : pageEn.getResults()){
                voList.add(AppVersionVO.poToVo(appVersionEntity));
            }

        }
        pageMap.put("total",pageEn.getTotalRecord());
        pageMap.put("rows",voList);
        return  super.ajaxReturnMap(OP_STATUS_SUCCESS,"",pageMap);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(AppVersionEntity entity){
        String id = "";
        //修改
        if(StringUtils.isNotBlank(entity.getId())){
            id = appVersionService.update(entity);
        }
        //新增
        else{
            id = appVersionService.save(entity);
        }
        return ajaxReturnMap(OP_STATUS_SUCCESS,"",id);
    }

    @RequestMapping("/get/{id}")
    @ResponseBody
    public Map<String,Object> getDetail(@PathVariable String id){
        AppVersionEntity entity = appVersionService.getById(id);
        AppVersionVO vo = AppVersionVO.poToVo(entity);
        return ajaxReturnMap(OP_STATUS_SUCCESS,"",vo);
    }

    @ResponseBody
    @RequestMapping("/remove")
    public Map<String,Object> remove(String ids){
        String[] idArray = ids.split(",");
        try{
            appVersionService.delete(idArray);
        }catch (Exception e){
            return ajaxReturnMap(OP_STATUS_FAILD,"删除失败",e.getMessage());
        }
        return ajaxReturnMap(OP_STATUS_SUCCESS,"删除成功",null);
    }



}
